/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.cancel;

import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.service.finance.api.WalletServiceApi;
import com.lxk.service.finance.enums.AccountTypeEnum;
import com.lxk.service.finance.request.UpdateAccountParam;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.StringUtils;

import static com.lxk.oms.flow.cancel.FlowCancelOrderNodeConst.YU_ROLLBACK_CANCEL_ORDER_NODE;

/**
 * 余额回滚节点 如果是余额支付的话，取消订单需要回退余额
 *
 * @author heyi
 * 2024/9/21
 */
@AllArgsConstructor
@LiteflowComponent(YU_ROLLBACK_CANCEL_ORDER_NODE)
public class YuRollBackNode extends NodeComponent {
    private final WalletServiceApi walletServiceApi;

    @Override
    public void process() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        UpdateAccountParam updateAccountParam = new UpdateAccountParam();
        updateAccountParam.setUserId(contextBean.getUserId());
        updateAccountParam.setAccountType(AccountTypeEnum.MONEY.getCode());
        updateAccountParam.setOrderSn(contextBean.getOrderNo());
        updateAccountParam.setAmount(omsOrderVo.getActReceiveAmount());
        updateAccountParam.setExtraInfo(StringUtils.format("订单取消, 退款:{}", omsOrderVo.getActPayPrice()));
        walletServiceApi.add(updateAccountParam);
    }

    @Override
    public void onSuccess() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
        optionLogEvent.setUserId(contextBean.getUserId());
        optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
        optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
        optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.YU_ROLL_BACK_ORDER.formatContent(omsOrderVo.getActReceiveAmount().toString()));
        optionLogEvent.setUserType(contextBean.getUserType());
        optionLogEvent.setMerId(omsOrderVo.getMerId());
        optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
        optionLogEvent.setTenantId(contextBean.getTenantId());
        EventHelper.publishEvent(optionLogEvent);
    }

    @Override
    public boolean isAccess() {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        Boolean success = contextBean.getSuccess();
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        String payType = omsOrderVo.getPayType();
        boolean equals = OrderEnums.PayType.YU.getCode().equals(payType);
        return success && equals;

    }
}
